package com.yuelu.community.community.util;

import jakarta.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;


/**
 * ClassName: MailClient
 * Description:
 *
 * @Author 文勇
 * @Create 2024/8/14 0:46
 * @Version 3.0
 */
@Component
public class MailClient {
    private static final Logger logger= LoggerFactory.getLogger(MailClient.class);

    @Autowired
    private JavaMailSender javaMailSender; //注入不进去，估计是依赖不是最新的

    @Value("${spring.mail.username}")
    private String from;

    public void sendMail(String subject,String to,String content){
        //ctrl+alt+t生成
        try {
            //模板
            MimeMessage mimeMessage =javaMailSender.createMimeMessage();
            //帮助类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content,true);
            javaMailSender.send(helper.getMimeMessage());
        } catch (Exception e) {
           logger.error("发送邮件失败："+e.getMessage());
        }
    }
}
